package com.nineteen;

import common.ListNode;

/**
 * @author ピカチュウ(皮卡啾)
 * @date 2021/7/14 10:54
 * @Description: 19. 删除链表的倒数第 N 个结点
 */
public class NineTeen {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode pre = new ListNode(0, head);
        ListNode left = pre;
        ListNode right = pre;

        while (n > 0) {
            right = right.next;
            --n;
        }

        while (right.next != null) {
            right = right.next;
            left = left.next;
        }
        left.next = left.next.next;
        return pre.next;
    }
}
